import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/remote/exception.dart';
import 'package:seedfour/app/routes/app_routes.dart';

bool handleException(ApiException exception,
    {bool Function(ApiException)? onError}) {
  if (onError?.call(exception) == true) {
    return true;
  }

  if (exception.code == 401) {
    Get.snackbar("提示", "登录过期，请重新登录",
        snackPosition: SnackPosition.TOP,
        duration: const Duration(seconds: 2), onTap: (v) {
      EasyLoading.dismiss();
      Get.offAllNamed(RouteGet.login);
    });
    return true;
  }
  EasyLoading.showError(exception.message ?? ApiException.unknownException);

  return false;
}
